package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @package com.leyou.auth.config
 * @description:
 * @author: 86157
 * @date 2019/7/7 17:15
 * @version: V1.0
 */
@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    private String pubKeyPath;
    private String priKeyPath;

    private PublicKey publicKey;
    private PrivateKey privateKey;


    //用户token的相关属性
    private UserTokenProperties user = new UserTokenProperties();

    @Data
    public class UserTokenProperties {
        /**
         * 过期时间
         */
        private int expire;
        /**
         * 存放token的cookie名
         */
        private String cookieName;
        /**
         * 存放token的cookie的domain
         */
        private String cookieDomain;

        /**
         * 最大刷新间隔
         */
        private Long maxRemainTime;
    }

    private AppTokenProperties app = new AppTokenProperties();

    @Data
    public class AppTokenProperties {
        private int expire;
        private Long id;
        private String secret;
        private String handleName;
    }

    /**
     * 获取公钥和私钥
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("获取公钥和私钥失败", e);
            throw new RuntimeException(e);
        }
    }
}